package helper.com.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import helper.com.domain.Manager;
import helper.com.mapper.ManagerMapper;
import helper.com.res.AjaxResponse;
import helper.com.service.ManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import helper.com.udo.RabbitmqUdo;
import helper.com.utils.RabbitmqUtil;
import helper.com.utils.RedisUtil;
import helper.com.vo.ManagerVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.Serializable;

import static helper.com.constants.CacheConstants.MANAGER_KEY_PREFIX;
import static helper.com.constants.RabbitmqConstants.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大忽悠
 * @since 2022年05月04日
 */
@Service
@RequiredArgsConstructor
public class ManagerServiceImp extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {
    private final RedisUtil redisUtil;
    private final RabbitmqUtil rabbitmqUtil;

    @Override
    public AjaxResponse saveManager(ManagerVO managerVO) {
        Manager manager = BeanUtil.copyProperties(managerVO, Manager.class);
        manager.setId(redisUtil.generatorUniqueId(MANAGER_KEY_PREFIX));
            rabbitmqUtil.send(MANAGER_EXCHANGE,MANAGER_ROUTE_KEY, RabbitmqUdo.builder()
                    .methodName("save").methodParamClass(Object.class).methodClass(IService.class).serviceClass(this.getClass()).obj(manager).build());
        return AjaxResponse.success(manager.getId());
    }

    @Override
    public AjaxResponse delManager(Long id) {
            rabbitmqUtil.send(MANAGER_EXCHANGE,MANAGER_ROUTE_KEY, RabbitmqUdo.builder()
                    .methodName("removeById").methodParamClass(Serializable.class).methodClass(IService.class).serviceClass(this.getClass()).obj(id).build());
        return AjaxResponse.success();
    }

    @Override
    public AjaxResponse getManager(Long id) {
        return AjaxResponse.success(getById(id));
    }
}
